package com.ruoyi.cms.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.cms.domain.CmsCatalog;
import com.ruoyi.cms.domain.CmsNavigation;
import com.ruoyi.cms.domain.CmsNews;
import com.ruoyi.cms.domain.HomeCatalog;
import com.ruoyi.cms.service.ICmsCatalogService;
import com.ruoyi.cms.service.ICmsNavigationService;
import com.ruoyi.cms.service.ICmsNewsService;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * 网站前端控制器
 */
@Controller
public class SiteController {

    @Autowired
    private ICmsNewsService cmsNewsService;
    @Autowired
    private ICmsCatalogService cmsCatalogService;
    @Autowired
    private ICmsNavigationService cmsNavigationService;
    @Autowired
    private ISysConfigService configService;

    /**
     * 首页
     */
    @GetMapping("/")
    public String index(Model model)
    {
        init(model);
        List<HomeCatalog> list = cmsNewsService.ListHomeCatalog();
        for(HomeCatalog h:list){
            model.addAttribute("cata_"+h.getCode(), h.getCatalog());
            model.addAttribute("list_"+h.getCode(), h.getList());
        }
        return templet(model, "index");
    }

    /**
     * 一级导航
     */
    @GetMapping("/nav-{navId}.html")
    public String nav(Model model, @PathVariable("navId") Long navId)
    {
        init(model);
        model.addAttribute("navId", navId);

        //判断是否为表单
        CmsNavigation cmsNavigation = cmsNavigationService.selectCmsNavigationById(navId);
        if(cmsNavigation.getType().equalsIgnoreCase(CmsNavigation.TYPE_FORM)){
            return templet(model, "form");
        }
        return newsList(model, Long.parseLong(cmsNavigation.getUri()));
    }

    /**
     * 访问首页，提示语
     */
    @GetMapping("/nav-{navId}-{cateId}.html")
    public String nav(Model model, @PathVariable("navId") Long navId, @PathVariable("cateId") Long cateId)
    {
        init(model);
        model.addAttribute("navId", navId);
        return newsList(model, cateId);
    }

    /**
     * 列表
     */
    @GetMapping("/list-{cateId}.html")
    public String list(Model model, @PathVariable("cateId") Long cateId)
    {
        init(model);
        model.addAttribute("navId", 0L);
        return newsList(model, cateId);
    }

    /**
     * 页面详细
     */
    @GetMapping("/view-{navId}-{newsId}.html")
    public String view(Model model, @PathVariable("navId") Long navId, @PathVariable("newsId") Long newsId)
    {
        init(model);
        model.addAttribute("navId", 0L);
        //内容
        CmsNews content = cmsNewsService.selectCmsNewsById(newsId);
        model.addAttribute("content", content);
        model.addAttribute("title", model.getAttribute("title")+" - " + content.getTitle());

        // 当前栏目
        CmsCatalog catalog = cmsCatalogService.selectCmsCatalogById(content.getCataId());
        CmsNews nc = new CmsNews();
        nc.setCataId(catalog.getId());
        model.addAttribute("catalog", catalog);

        //推荐内容
        nc.setCataId(null);
        nc.setRecommend("Y");;
        List<CmsNews> recommendList = cmsNewsService.selectCmsNewsList(nc);
        model.addAttribute("recommendList", recommendList);

        String view = "view";
        if(catalog.getType().equalsIgnoreCase(CmsCatalog.TYPE_ALBUM)){
            view += "-album";
        }else if(catalog.getType().equalsIgnoreCase(CmsCatalog.TYPE_FILES)){
            view += "-files";
        }else if(catalog.getType().equalsIgnoreCase(CmsCatalog.TYPE_LINKS)){
            view += "-links";
        }

        //上级栏目
        if(catalog.getPid().intValue() != 0){
            //上级栏目
            model.addAttribute("parent", cmsCatalogService.selectCmsCatalogById(catalog.getPid()));

            //同级栏目
            CmsCatalog cc = new CmsCatalog();
            PageHelper.startPage(1, 100, "sorts ASC");
            cc.setPid(catalog.getPid());
            List<CmsCatalog> subList = cmsCatalogService.selectCmsCatalogList(cc);
            model.addAttribute("subs", subList);

            view += "-sider";
        }
        return templet(model, view);
    }

    /**
     * 列表页面
     * @param model
     * @param cateId
     * @return
     */
    private String newsList(Model model, Long cateId){

        // 当前栏目
        CmsCatalog catalog = cmsCatalogService.selectCmsCatalogById(cateId);
        model.addAttribute("catalog", catalog);
        model.addAttribute("title", model.getAttribute("title")+" - " + catalog.getTitle());

        if(catalog.getType().equalsIgnoreCase(CmsCatalog.TYPE_SINGLE)){
            return view(model, Long.parseLong(model.getAttribute("navId").toString()), cateId);
        }

        CmsNews nc = new CmsNews();
        nc.setCataId(catalog.getId());
        //置顶内容
        nc.setToped("Y");;
        List<CmsNews> topList = cmsNewsService.selectCmsNewsList(nc);
        model.addAttribute("topList", topList);

        //分页
        page(catalog.getPager().intValue());

        //非置顶列表
        nc.setToped("N");;
        List<CmsNews> list = cmsNewsService.selectCmsNewsList(nc);
        PageInfo pager = new PageInfo(list);
        model.addAttribute("pager", pager);
        model.addAttribute("list", list);

        //根据类别选择视图模板
        String view = "list";
        if(catalog.getType().equalsIgnoreCase(CmsCatalog.TYPE_ALBUM)){
            view += "-album";
        }else if(catalog.getType().equalsIgnoreCase(CmsCatalog.TYPE_FILES)){
            view += "-files";
        }else if(catalog.getType().equalsIgnoreCase(CmsCatalog.TYPE_LINKS)){
            view += "-links";
        }

        //上级栏目
        if(catalog.getPid().intValue() != 0){
            //上级栏目
            model.addAttribute("parent", cmsCatalogService.selectCmsCatalogById(catalog.getPid()));

            //同级栏目
            CmsCatalog cc = new CmsCatalog();
            PageHelper.startPage(1, 100, "sorts ASC");
            cc.setPid(catalog.getPid());
            List<CmsCatalog> subList = cmsCatalogService.selectCmsCatalogList(cc);
            model.addAttribute("subs", subList);

            view += "-sider";
        }
        return templet(model, view);
    }


    /**
     * 分页
     * @param size
     */
    private void page(int size){
        String page = ServletUtils.getParameter("page");
        if(StringUtils.isEmpty(page)){
            page = "1";
        }
        PageHelper.startPage(Integer.parseInt(page), size);
    }

    /**
     * 初始化通用信息
     */
    private void init(Model model){
        model.addAttribute("title", configService.selectConfigByKey("site.title"));
        model.addAttribute("keywords", configService.selectConfigByKey("site.keywords"));
        model.addAttribute("description", configService.selectConfigByKey("site.description"));
        model.addAttribute("beian", configService.selectConfigByKey("site.home.beian"));
        model.addAttribute("orgName", configService.selectConfigByKey("site.org.name"));
        model.addAttribute("orgAddress", configService.selectConfigByKey("site.org.address"));
        model.addAttribute("orgLinks", configService.selectConfigByKey("site.org.links"));
        model.addAttribute("navList", cmsNavigationService.listNavigation());
        model.addAttribute("_tpl", "default");
    }

    /**
     * 返回模板
     * @param model
     * @param tpl
     * @return
     */
    private String templet(Model model, String tpl){
        return model.getAttribute("_tpl") + "/"+tpl;
    }
}
